package com.qingcheng.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.CommentEvaluationMapper;
import com.qingcheng.dao.CommentLogMapper;
import com.qingcheng.dao.CommentMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.comment.Comment;
import com.qingcheng.pojo.comment.CommentEvaluation;
import com.qingcheng.pojo.comment.CommentLog;
import com.qingcheng.service.comment.CommentEvaluationService;
import com.qingcheng.service.comment.CommentService;
import com.qingcheng.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommentEvaluationServiceImplHt implements CommentEvaluationService {

    @Autowired
    private CommentEvaluationMapper commentEvaluationMapper;

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private CommentLogMapper commentLogMapper;
    /**
     * 返回全部记录
     * @return
     */
    public List<CommentEvaluation> findAll() {
        return commentEvaluationMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<CommentEvaluation> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<CommentEvaluation> commentEvaluations = (Page<CommentEvaluation>) commentEvaluationMapper.selectAll();
        return new PageResult<CommentEvaluation>(commentEvaluations.getTotal(),commentEvaluations.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<CommentEvaluation> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return commentEvaluationMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<CommentEvaluation> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<CommentEvaluation> commentEvaluations = (Page<CommentEvaluation>) commentEvaluationMapper.selectByExample(example);
        return new PageResult<CommentEvaluation>(commentEvaluations.getTotal(),commentEvaluations.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public CommentEvaluation findById(String id) {
        return commentEvaluationMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param commentEvaluation
     */
    public void add(CommentEvaluation commentEvaluation) {
        commentEvaluationMapper.insert(commentEvaluation);
    }

    /**
     * 修改
     * @param commentEvaluation
     */
    public void update(CommentEvaluation commentEvaluation) {

       commentEvaluationMapper.updateByPrimaryKeySelective(commentEvaluation);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(String id) {
        commentEvaluationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(CommentEvaluation.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // id
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andLike("id","%"+searchMap.get("id")+"%");
            }
            // 评论用户
            if(searchMap.get("uesrname")!=null && !"".equals(searchMap.get("uesrname"))){
                criteria.andLike("uesrname","%"+searchMap.get("uesrname")+"%");
            }
            // 评论评价内容
            if(searchMap.get("message")!=null && !"".equals(searchMap.get("message"))){
                criteria.andLike("message","%"+searchMap.get("message")+"%");
            }

            // 0显示，1隐藏，2删除
            if(searchMap.get("status")!=null ){
                criteria.andEqualTo("status",searchMap.get("status"));
            }
            // 评论id
            if(searchMap.get("commentId")!=null ){
                criteria.andEqualTo("commentId",searchMap.get("commentId"));
            }

        }
        return example;
    }

}
